package com.cleaningcloud.cld.utils;

import com.cleaningcloud.cld.domain.CldPayDetails;
import com.cleaningcloud.cld.domain.mini.CheckInResult;
import com.cleaningcloud.cld.domain.vo.CldAttendanceVo;
import com.cleaningcloud.cld.domain.vo.CldProjectPostSetVo;
import net.sf.cglib.core.Local;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.*;
import java.time.temporal.ChronoUnit;

/**
 * 考勤时间转换
 */
public class AttendanceConversionUtils {

    /**
     * 考勤请假时间计算
     * 加班时长计算
     *
     * @param cldAttendanceVo
     */
    public static void AttendanceConversion(CldAttendanceVo cldAttendanceVo) {
        // 单位
        BigDecimal unit = new BigDecimal("60.0");
        // 是否请假
        if (cldAttendanceVo.getIsLeave() == 1) {
            // 请假时长
            BigDecimal leaveHours = new BigDecimal("0.0");
            long minutesDiff = ChronoUnit.MINUTES.between(cldAttendanceVo.getLeaveStartTime(), cldAttendanceVo.getLeaveEndTime());
            leaveHours = new BigDecimal(minutesDiff).divide(unit, 1, RoundingMode.HALF_UP);
            cldAttendanceVo.setLeaveHours(leaveHours);
        } else if (cldAttendanceVo.getIsInThisProject() == 1) {
            // 判断是否本项目加班
            BigDecimal minutesDiff = new BigDecimal("0.0");
            if (cldAttendanceVo.getThisStartTime() != null && cldAttendanceVo.getThisEndTime() != null) {
                long minutes = ChronoUnit.MINUTES.between(cldAttendanceVo.getEchoGoToWorkTimeOvertime(), cldAttendanceVo.getEchoGoOffWorkTimeOvertime());
                minutesDiff = new BigDecimal(minutes).divide(unit, 1, RoundingMode.HALF_UP);
            }
            cldAttendanceVo.setInThisHours(minutesDiff);
        } else if (cldAttendanceVo.getIsOutExtProject() == 1) {
            // 判断是否外出加班
            // 判断是否本项目加班
            BigDecimal minutesDiff = new BigDecimal("0.0");
            if (cldAttendanceVo.getExtStartTime() != null && cldAttendanceVo.getExtEndTime() != null) {
                long minutes = ChronoUnit.MINUTES.between(cldAttendanceVo.getEchoGoToWorkTimeOvertime(), cldAttendanceVo.getEchoGoOffWorkTimeOvertime());
                minutesDiff = new BigDecimal(minutes).divide(unit, 1, RoundingMode.HALF_UP);
            }
            cldAttendanceVo.setOutExtHours(minutesDiff);
        }
    }

    public static CldPayDetails LeaveSalaryCalculate(CldAttendanceVo attendance, CldProjectPostSetVo cldProjectPost) {
        // 获取当天信息
        LocalDate localDate = LocalDate.now();
        // 获取当月天数
        int daysInMonth = localDate.lengthOfMonth();
        // 得出请假时长
        AttendanceConversionUtils.AttendanceConversion(attendance);
        boolean isAll = attendance.getLeaveHours().doubleValue() >= (cldProjectPost.getIsClass() != null && cldProjectPost.getIsClass() == 1 ?  16 : 8);
        if (cldProjectPost.getPostWages() == null) {
            cldProjectPost.setPostWages(new BigDecimal("1900"));
        }
        // 计算日平均工资
        BigDecimal dailyAverage = cldProjectPost.getPostWages().divide(BigDecimal.valueOf(daysInMonth), 2, RoundingMode.HALF_UP);
        if (daysInMonth == localDate.getDayOfMonth()) {
            dailyAverage = cldProjectPost.getPostWages().subtract(dailyAverage.multiply(BigDecimal.valueOf(daysInMonth - 1)));
        }
        BigDecimal leaveAmount = new BigDecimal("0.0");
        if (!isAll) {
            // 计算小时平均工资
            BigDecimal houseAmount = dailyAverage.divide(BigDecimal.valueOf(8), 2, RoundingMode.HALF_UP);
            // 根据请假时长计算当日应扣款金额
            leaveAmount = houseAmount.multiply(attendance.getLeaveHours()).negate();
        } else {
            leaveAmount = dailyAverage.negate();
        }

        // 是否通班 扣除薪资 * 2
        if(cldProjectPost.getIsClass() != null && cldProjectPost.getIsClass() == 1){
            leaveAmount = leaveAmount.multiply(BigDecimal.valueOf(2));
        }
        // 插入薪酬明细
        CldPayDetails cldPayDetails = new CldPayDetails();
        cldPayDetails.setBusinessId(attendance.getAttendanceId());
        cldPayDetails.setProjectId(attendance.getProjectId());
        cldPayDetails.setStaffId(attendance.getStaffId());
        cldPayDetails.setProjectPostId(cldProjectPost.getProjectPostName());
        cldPayDetails.setPayType(1L);
        cldPayDetails.setMoney(leaveAmount);
        cldPayDetails.setIsAlgorithm(2L);
        cldPayDetails.setYear(attendance.getAttendanceYear());
        cldPayDetails.setMonth(attendance.getAttendanceMonth());
        cldPayDetails.setDay(attendance.getAttendanceDay());
        cldPayDetails.setRemark("请假"+attendance.getLeaveHours()+"小时,扣款金额"+leaveAmount);
        return cldPayDetails;
    }

    /**
     * 是否可打卡
     *
     * @param attendanceVo
     * @param lng
     * @param lat
     * @return
     */
    public static CheckInResult isCheckIn(CldAttendanceVo attendanceVo, BigDecimal lng, BigDecimal lat) {

        CheckInResult checkInResult = new CheckInResult();
        // 是否休息并且无加班
        if (attendanceVo.getIsRest() == 1 && attendanceVo.getIsInThisProject() == 0 && attendanceVo.getIsOutExtProject() == 0) {
            checkInResult.setCheckIn(false);
            checkInResult.setErrorMsg("今日休息,无需打卡！");
            return checkInResult;
        }
        // 经纬度为空
        if (lng == null || lat == null) {
            checkInResult.setCheckIn(false);
            checkInResult.setErrorMsg("请开启定位！");
            return checkInResult;
        }
        // 项目暂未配置打卡地点
        if (attendanceVo.getInLat() == null || attendanceVo.getInLng() == null) {
            checkInResult.setCheckIn(false);
            checkInResult.setErrorMsg("项目暂未配置打卡地点！");
            return checkInResult;
        }
        // 本项目打卡距离
        checkInResult.setInProjectDistance(PositioningUtil.distance_2(attendanceVo.getInLng().doubleValue(), attendanceVo.getInLat().doubleValue(), lng.doubleValue(), lat.doubleValue()));
        // 是否存在外项目加班
        if (attendanceVo.getIsOutExtProject() == 1) {
            // 外项目未配置打卡地点
            if (attendanceVo.getOutLng() == null || attendanceVo.getOutLat() == null) {
                checkInResult.setCheckIn(false);
                checkInResult.setErrorMsg("外项目暂未配置打卡地点！");
                return checkInResult;
            }
            checkInResult.setOutProjectDistance(PositioningUtil.distance_2(attendanceVo.getOutLng().doubleValue(), attendanceVo.getOutLat().doubleValue(), lng.doubleValue(), lat.doubleValue()));
        }
        // 是否在本项目打卡范围内
        if (checkInResult.getInProjectDistance() <= attendanceVo.getInRange()) {
            checkInResult.setCheckIn(true);
            // 是否存在本项目加班
            checkInResult.setCheckInType(attendanceVo.getIsInThisProject() == 1 ? 3 : 1);
        } else if (checkInResult.getOutProjectDistance() > 0 && checkInResult.getOutProjectDistance() <= attendanceVo.getOutRange()) {
            // 如外项目距离 > 0
            checkInResult.setCheckInType(2);
            checkInResult.setCheckIn(true);
        } else {
            checkInResult.setErrorMsg("请在指定范围内进行打卡");
        }
        // 如都在范围内 则取最小值
        if ((checkInResult.getInProjectDistance() > 0 && checkInResult.getInProjectDistance() <= attendanceVo.getInRange()) && (checkInResult.getOutProjectDistance() > 0 && checkInResult.getOutProjectDistance() <= attendanceVo.getOutRange())) {
            checkInResult.setCheckInType(checkInResult.getInProjectDistance() < checkInResult.getOutProjectDistance() ? 1 : 2);
        }
        // 可打卡
        if (checkInResult.isCheckIn()) {
//            // 是否在时间范围内
//            if (checkInResult.getCheckInType() == 1) {
//                inCheckIn(checkInResult, attendanceVo);
//            } else {
//                outCheckIn(checkInResult, attendanceVo);
//            }
            // 存在加班 计算是前置加班 还是后置加班
            if (checkInResult.getCheckInType() != 1) {
                if (attendanceVo.getEchoGoToWorkTime1() != null && LocalTime.from(attendanceVo.getEchoGoOffWorkTimeOvertime()).isBefore(attendanceVo.getEchoGoToWorkTime1())) {
                    checkInResult.setOvertimeType(1);
                } else {
                    checkInResult.setOvertimeType(2);
                }
            }

        }
        return checkInResult;
    }

    /**
     * 本项目打卡判断
     *
     * @param checkInResult
     * @param cldAttendance
     */
    public static void inCheckIn(CheckInResult checkInResult, CldAttendanceVo cldAttendance) {
        LocalDate localDate = LocalDate.now();
        // 获取当前时间时分秒
        LocalTime nowTime = LocalTime.now();
        if (cldAttendance.getAttendanceDay() != localDate.getDayOfMonth()) {
            checkInResult.setErrorMsg("当前时间与系统时间不匹配！");
            checkInResult.setCheckIn(false);
        } else {
            // 无休息
            if (cldAttendance.getIsRest() == 0) {
                // 第一个班次不为空
                if (cldAttendance.getEchoGoToWorkTime1() != null && cldAttendance.getEchoGoOffWorkTime1() != null) {
                    // 当前时间大于上班时间 并且小于下班时间
                    if (nowTime.isAfter(cldAttendance.getEchoGoToWorkTime1()) && nowTime.isBefore(cldAttendance.getEchoGoOffWorkTime1())) {
                        checkInResult.setCheckIn(false);
                    }
                }
                // 第二个班次不为空
                if (cldAttendance.getEchoGoToWorkTime2() != null && cldAttendance.getEchoGoOffWorkTime2() != null) {
                    // 当前时间大于上班时间 并且小于下班时间
                    if (nowTime.isAfter(cldAttendance.getEchoGoToWorkTime2()) && nowTime.isBefore(cldAttendance.getEchoGoOffWorkTime2())) {
                        checkInResult.setCheckIn(false);
                    }
                }
                // 第三个班次不为空
                if (cldAttendance.getEchoGoToWorkTime3() != null && cldAttendance.getEchoGoOffWorkTime3() != null) {
                    // 当前时间大于上班时间 并且小于下班时间
                    if (nowTime.isAfter(cldAttendance.getEchoGoToWorkTime3()) && nowTime.isBefore(cldAttendance.getEchoGoOffWorkTime3())) {
                        checkInResult.setCheckIn(false);
                    }
                }
            } else {
                checkInResult.setCheckIn(false);
                checkInResult.setErrorMsg("今日休息！");
            }
        }
    }

    /**
     * 加班打卡判断
     *
     * @param checkInResult
     * @param cldAttendance
     */
    public static void outCheckIn(CheckInResult checkInResult, CldAttendanceVo cldAttendance) {
        LocalDate localDate = LocalDate.now();
        // 获取当前时间时分秒
        LocalDateTime nowTime = LocalDateTime.now();
        if (cldAttendance.getAttendanceDay() != localDate.getDayOfMonth()) {
            checkInResult.setErrorMsg("当前时间与系统时间不匹配！");
            checkInResult.setCheckIn(false);
        } else {
            // 加班时间不为空
            if (cldAttendance.getEchoGoToWorkTimeOvertime() != null && cldAttendance.getEchoGoOffWorkTimeOvertime() != null) {
                checkInResult.setCheckIn(nowTime.isAfter(cldAttendance.getEchoGoToWorkTimeOvertime()) && nowTime.isBefore(cldAttendance.getEchoGoOffWorkTimeOvertime()));
            }
        }
    }

    /**
     * 考勤打卡
     *
     * @param cldAttendance
     * @param checkInResult
     */
    public static void checkIn(CldAttendanceVo cldAttendance, CheckInResult checkInResult) {
        // 获取当前时间时分秒
        LocalDateTime nowTime = LocalDateTime.now();
        boolean flag = true;
        // 如为外项目打卡 直接走加班打卡
        if (checkInResult.getCheckInType() == 2) {
            overtimeCheckIn(cldAttendance, LocalTime.from(nowTime));
        } else {
            // 正常考勤
            if (checkInResult.getCheckInType() == 1 && cldAttendance.getIsRest() == 0) {
                attCheckIn(cldAttendance, LocalTime.from(nowTime), flag);
            }
            // 前后加班
            if (checkInResult.getCheckInType() == 3) {
                if (checkInResult.getOvertimeType() == 1) {
                    boolean isOvertime = overtimeCheckIn(cldAttendance, LocalTime.from(nowTime));
                    if (isOvertime && cldAttendance.getIsRest() == 0) {
                        attCheckIn(cldAttendance, LocalTime.from(nowTime), flag);
                    }
                } else {
                    if(cldAttendance.getIsRest() == 0){
                        boolean isOvertime = attCheckIn(cldAttendance, LocalTime.from(nowTime), flag);
                        if (isOvertime) {
                            overtimeCheckIn(cldAttendance, LocalTime.from(nowTime));
                        }
                    }else{
                        overtimeCheckIn(cldAttendance, LocalTime.from(nowTime));
                    }
                }
            }
        }
    }

    // 正常考勤
    public static boolean attCheckIn(CldAttendanceVo cldAttendance, LocalTime nowTime, boolean flag) {
        boolean isOvertime = false;

        //特殊班次(如：影城)
        if(null != cldAttendance.getIsSpecial() && cldAttendance.getIsSpecial() == 1){
            //已经打了第一个上班卡
            if(null != cldAttendance.getGoToWorkTime1()){
                //如果跨天打卡
                if(cldAttendance.getIsNextTime1() == 1){
                    //将LocalTime加入日期
                    LocalDate date = LocalDate.now();
                    LocalDateTime dateTime = date.atTime(nowTime);
                    //转换为特定时区的时间,例如使用UTC时区
                    ZonedDateTime zonedTime = dateTime.atZone(ZoneId.of("UTC"));
                    //获取当天开始时间和结束时间
                    LocalDateTime startOfDay = LocalDateTime.of(LocalDate.of(cldAttendance.getAttendanceYear(),cldAttendance.getAttendanceMonth(), cldAttendance.getAttendanceDay()),LocalTime.MIN);
                    LocalDateTime nedOfDay = LocalDateTime.of(LocalDate.of(cldAttendance.getAttendanceYear(),cldAttendance.getAttendanceMonth(), cldAttendance.getAttendanceDay()),LocalTime.MAX);
                    //将特定时间转换回ZonedDateTime以便比较
                    ZonedDateTime zonedStartTime = startOfDay.atZone(ZoneId.of("UTC"));
                    ZonedDateTime zonedEndTime = nedOfDay.atZone(ZoneId.of("UTC"));
                    //跨天打下班卡
                    if(zonedTime.isAfter(zonedEndTime) && nowTime.isAfter(cldAttendance.getEchoGoOffWorkTime1())){
                        cldAttendance.setGoOffWorkTime1(nowTime);
                    }else{
                        throw new RuntimeException("本班次上下班时间为[" + cldAttendance.getEchoGoToWorkTime1() + " - " + cldAttendance.getEchoGoOffWorkTime1() + "]");
                    }
                }else{
                    if(nowTime.isAfter(cldAttendance.getEchoGoOffWorkTime1())){
                        cldAttendance.setGoOffWorkTime1(nowTime);
                    }else{
                        throw new RuntimeException("本班次上下班时间为[" + cldAttendance.getEchoGoToWorkTime1() + " - " + cldAttendance.getEchoGoOffWorkTime1() + "]");
                    }
                }
            //已经打了第二个上班卡
            }else if(null != cldAttendance.getGoToWorkTime2()){
                //如果跨天打卡
                if(cldAttendance.getIsNextTime2() == 1){
                    //将LocalTime加入日期
                    LocalDate date = LocalDate.now();
                    LocalDateTime dateTime = date.atTime(nowTime);
                    //转换为特定时区的时间,例如使用UTC时区
                    ZonedDateTime zonedTime = dateTime.atZone(ZoneId.of("UTC"));
                    //获取当天开始时间和结束时间
                    LocalDateTime startOfDay = LocalDateTime.of(LocalDate.of(cldAttendance.getAttendanceYear(),cldAttendance.getAttendanceMonth(), cldAttendance.getAttendanceDay()),LocalTime.MIN);
                    LocalDateTime nedOfDay = LocalDateTime.of(LocalDate.of(cldAttendance.getAttendanceYear(),cldAttendance.getAttendanceMonth(), cldAttendance.getAttendanceDay()),LocalTime.MAX);
                    //将特定时间转换回ZonedDateTime以便比较
                    ZonedDateTime zonedStartTime = startOfDay.atZone(ZoneId.of("UTC"));
                    ZonedDateTime zonedEndTime = nedOfDay.atZone(ZoneId.of("UTC"));
                    //跨天打下班卡
                    if(zonedTime.isAfter(zonedEndTime) && nowTime.isAfter(cldAttendance.getEchoGoOffWorkTime2())){
                        cldAttendance.setGoOffWorkTime2(nowTime);
                    }else{
                        throw new RuntimeException("本班次上下班时间为[" + cldAttendance.getEchoGoToWorkTime2() + " - " + cldAttendance.getEchoGoOffWorkTime2() + "]");
                    }
                }else {
                    if (nowTime.isAfter(cldAttendance.getEchoGoOffWorkTime2())) {
                        cldAttendance.setGoOffWorkTime2(nowTime);
                    } else {
                        throw new RuntimeException("本班次上下班时间为[" + cldAttendance.getEchoGoToWorkTime2() + " - " + cldAttendance.getEchoGoOffWorkTime2() + "]");
                    }
                }
            //已经打了第三个上班卡
            }else if(null != cldAttendance.getGoToWorkTime3()) {
                //如果跨天打卡
                if(cldAttendance.getIsNextTime3() == 1){
                    //将LocalTime加入日期
                    LocalDate date = LocalDate.now();
                    LocalDateTime dateTime = date.atTime(nowTime);
                    //转换为特定时区的时间,例如使用UTC时区
                    ZonedDateTime zonedTime = dateTime.atZone(ZoneId.of("UTC"));
                    //获取当天开始时间和结束时间
                    LocalDateTime startOfDay = LocalDateTime.of(LocalDate.of(cldAttendance.getAttendanceYear(),cldAttendance.getAttendanceMonth(), cldAttendance.getAttendanceDay()),LocalTime.MIN);
                    LocalDateTime nedOfDay = LocalDateTime.of(LocalDate.of(cldAttendance.getAttendanceYear(),cldAttendance.getAttendanceMonth(), cldAttendance.getAttendanceDay()),LocalTime.MAX);
                    //将特定时间转换回ZonedDateTime以便比较
                    ZonedDateTime zonedStartTime = startOfDay.atZone(ZoneId.of("UTC"));
                    ZonedDateTime zonedEndTime = nedOfDay.atZone(ZoneId.of("UTC"));
                    //跨天打下班卡
                    if(zonedTime.isAfter(zonedEndTime) && nowTime.isAfter(cldAttendance.getEchoGoOffWorkTime3())){
                        cldAttendance.setGoOffWorkTime3(nowTime);
                    }else{
                        throw new RuntimeException("本班次上下班时间为[" + cldAttendance.getEchoGoToWorkTime3() + " - " + cldAttendance.getEchoGoOffWorkTime3() + "]");
                    }
                }else {
                    if (nowTime.isAfter(cldAttendance.getEchoGoOffWorkTime3())) {
                        cldAttendance.setGoOffWorkTime3(nowTime);
                    } else {
                        throw new RuntimeException("本班次上下班时间为[" + cldAttendance.getEchoGoToWorkTime3() + " - " + cldAttendance.getEchoGoOffWorkTime3() + "]");
                    }
                }
            }else{
                //所有班次上班卡均为打卡
                //第一个上班卡判断
                if(nowTime.isBefore(cldAttendance.getEchoGoToWorkTime1())){
                    cldAttendance.setGoToWorkTime1(nowTime);
                //第二个上班卡判断
                }else if(nowTime.isBefore(cldAttendance.getEchoGoToWorkTime2())){
                    cldAttendance.setGoToWorkTime2(nowTime);
                //第三个上班卡判断
                }else if(nowTime.isBefore(cldAttendance.getEchoGoToWorkTime3())){
                    cldAttendance.setGoToWorkTime3(nowTime);
                }else {
                    throw new RuntimeException("请满足以上班次上班时间打卡");
                }
            }
        }else{
            // 第一个班次不为空
            if (cldAttendance.getEchoGoToWorkTime1() != null && cldAttendance.getEchoGoOffWorkTime1() != null) {
                // 当前时间小于应上班打卡时间
                if (nowTime.isBefore(cldAttendance.getEchoGoToWorkTime1())) {
                    if (cldAttendance.getGoToWorkTime1() == null) {
                        cldAttendance.setGoToWorkTime1(nowTime);
                    } else {
                        isOvertime = true;
                    }
                    flag = false;
                // 当前时间大于应下班打卡时间
                } else if (nowTime.isAfter(cldAttendance.getEchoGoOffWorkTime1())) {
                    if (cldAttendance.getEchoGoToWorkTime2() != null) {
                        // 未打下班卡
                        if (cldAttendance.getGoOffWorkTime1() == null) {
                            // 当前时间小于第三班次上班时间
                            if (nowTime.isBefore(cldAttendance.getEchoGoToWorkTime2())) {
                                cldAttendance.setGoOffWorkTime1(nowTime);
                                flag = false;
                            }
                        } else {
                            isOvertime = true;
                        }
                    } else if (cldAttendance.getEchoGoToWorkTime3() != null) {
                        // 未打下班卡
                        if (cldAttendance.getGoOffWorkTime1() == null) {
                            // 当前时间小于第三班次上班时间
                            if (nowTime.isBefore(cldAttendance.getEchoGoToWorkTime3())) {
                                cldAttendance.setGoOffWorkTime1(nowTime);
                                flag = false;
                            }
                        } else {
                            isOvertime = true;
                        }
                    } else {
                        if (cldAttendance.getGoOffWorkTime1() == null) {
                            cldAttendance.setGoOffWorkTime1(nowTime);
                        } else {
                            isOvertime = true;
                        }
                        flag = false;
                    }
                } else {
                    throw new RuntimeException("本班次上下班时间为[" + cldAttendance.getEchoGoToWorkTime1() + " - " + cldAttendance.getEchoGoOffWorkTime1() + "]");
                }
            }
            // 第二班次判断
            if (cldAttendance.getEchoGoToWorkTime2() != null && cldAttendance.getEchoGoOffWorkTime2() != null && flag) {
                // 当前时间小于第二个班次上班时间
                if (nowTime.isBefore(cldAttendance.getEchoGoToWorkTime2())) {
                    if (cldAttendance.getGoToWorkTime2() == null) {
                        cldAttendance.setGoToWorkTime2(nowTime);
                    } else {
                        isOvertime = true;
                    }
                    flag = false;
                } else if (nowTime.isAfter(cldAttendance.getEchoGoOffWorkTime2())) {
                    if (cldAttendance.getEchoGoToWorkTime3() != null) {
                        // 未打下班卡
                        if (cldAttendance.getGoOffWorkTime2() == null) {
                            // 当前时间小于第三班次上班时间
                            if (nowTime.isBefore(cldAttendance.getEchoGoToWorkTime3())) {
                                cldAttendance.setGoOffWorkTime2(nowTime);
                                flag = false;
                            }
                        } else {
                            isOvertime = true;
                        }
                    } else {
                        if (cldAttendance.getGoOffWorkTime2() == null) {
                            cldAttendance.setGoOffWorkTime2(nowTime);
                        } else {
                            isOvertime = true;
                        }
                        flag = false;
                    }
                } else {
                    throw new RuntimeException("本班次上下班时间为[" + cldAttendance.getEchoGoToWorkTime2() + " - " + cldAttendance.getEchoGoOffWorkTime2() + "]");
                }
            }
            // 第三班次判断
            if (cldAttendance.getEchoGoToWorkTime3() != null && cldAttendance.getEchoGoOffWorkTime3() != null && flag) {
                // 当前时间小于上班时间 且 没有打上班卡
                if (nowTime.isBefore(cldAttendance.getEchoGoToWorkTime3())) {
                    if (cldAttendance.getGoToWorkTime3() == null) {
                        cldAttendance.setGoToWorkTime3(nowTime);
                    } else {
                        isOvertime = true;
                    }
                    flag = false;
                } else if (nowTime.isAfter(cldAttendance.getEchoGoOffWorkTime3())) {
                    if (cldAttendance.getGoOffWorkTime3() == null) {
                        cldAttendance.setGoOffWorkTime3(nowTime);
                    } else {
                        isOvertime = true;
                    }
                    flag = false;
                } else {
                    throw new RuntimeException("本班次上下班时间为[" + cldAttendance.getEchoGoToWorkTime3() + " - " + cldAttendance.getEchoGoOffWorkTime3() + "]");
                }
            }
        }
        return isOvertime;
    }

    /**
     * 加班打卡
     *
     * @param cldAttendance
     * @param nowTime
     */
    public static boolean overtimeCheckIn(CldAttendanceVo cldAttendance, LocalTime nowTime) {
        // 加班时间不为空
        if (cldAttendance.getEchoGoToWorkTimeOvertime() != null && nowTime.isBefore(LocalTime.from(cldAttendance.getEchoGoToWorkTimeOvertime()))) {
            if (cldAttendance.getIsInThisProject() == 1) {
                if (cldAttendance.getThisStartTime() == null) {
                    cldAttendance.setThisStartTime(nowTime);
                    return false;
                } else {
                    return true;
                }
            } else {
                if (cldAttendance.getExtStartTime() == null) {
                    cldAttendance.setExtStartTime(nowTime);
                    return false;
                } else {
                    return true;
                }
            }
        } else if (cldAttendance.getEchoGoOffWorkTimeOvertime() != null && nowTime.isAfter(LocalTime.from(cldAttendance.getEchoGoOffWorkTimeOvertime()))) {
            if (cldAttendance.getIsInThisProject() == 1) {
                if (cldAttendance.getThisEndTime() == null) {
                    cldAttendance.setThisEndTime(nowTime);
                    return false;
                } else {
                    return true;
                }
            } else {
                if (cldAttendance.getExtEndTime() == null) {
                    cldAttendance.setExtEndTime(nowTime);
                    return false;
                } else {
                    return true;
                }
            }
        } else {
            throw new RuntimeException("当前时段正处于加班时段内！");
        }
    }
}
